import { ValidationService, ValidationType } from '../../framework';
import { LoggerService, LoggerInfo } from './../../framework/logger/logger.service';
import { GeneralServiceProvider } from './../../framework/data-provider/provider.general';
import { Input } from '@angular/core';
// import { TranslateService } from 'ng2-translate';
import { BaseLoginService } from './login.baseService';
import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
import { SelectItem } from 'primeng/primeng';
import { User, ILoginConfig } from './module/login.module';
export class BaseLogin {
    public user: User;
    @Input() loginConfig: ILoginConfig;
    languages: SelectItem[];
    // options: SelectItem[];
    loginForm: FormGroup;
    language: string = 'zh-cn';
    isLanguageForEN: boolean = false;
    public _fb: FormBuilder;
    // public _translate: TranslateService;
    public _mock: GeneralServiceProvider;
    public _logger: LoggerService;
    // public _validate: ValidationService;
    constructor(
        baseLoginService?: BaseLoginService,
        _fb?: FormBuilder,
        // _translate?: TranslateService,
        _mock?: GeneralServiceProvider,
        _logger?: LoggerService,
        private _validate?: ValidationService) {
        this.user = baseLoginService.user;
        this.user = new User(null, null);
        this._fb = _fb;
        // this._translate = _translate;
        this._mock = _mock;
        this._logger = _logger;
        // this._validate = _validate;
    }
    baseOnChanges() {
        // this.options = this.loginConfig.project;
        this.languages = this.loginConfig.languages;
        // this.user.role = this.options[0].value;
    }
    baseOnSubmit() {
        let validateTypes = [
            ValidationType.NotEmpty,
            ValidationType.NotNull,
            // {Type: ValidationType.LengthScope,input:[1,∞]}
        ]
        let validateByName = [{ type: ValidationType.LengthScope, input: '[3, ~]' }];

        //validation input is null and empty
        let resultValidate = this._validate.checkingAllPass(validateTypes, this.user.name, this.user.pwd);
        let resultValidateByName = this._validate.checkingAllPass(validateByName, this.user.name);
        // if not pass and show kind info
        if (!resultValidate || !resultValidateByName) {
            this._logger.error(new LoggerInfo('LoginComponent', 'Validation not pass, please rewrite input', 'kind error'));
            return false;
        }
        let para = {
            //Items: undefined,
            where: `username = '${this.user.name}'`
        }
        let self = this;
        this._mock.get(para, function (data) {
            if (data && data.length > 0) {
                console.log(self.user);
                self.loginConfig.onSubmit(self.user);
            } else {
                self._logger.info(new LoggerInfo('Login', 'user or password is wrong', null))
            }
        }, function (data) {
            console.log(data);
        });
    }
    baseOnInit() {
        this.loginForm = this._fb.group({
            'name': new FormControl('', Validators.required),
            'pwd': new FormControl('', Validators.required),
            // 'options': new FormControl(''),
            'languages': new FormControl('')
        });
    }
    baseChangeLanguage(e) {
        this.language === 'en-us' ? this.isLanguageForEN = true : this.isLanguageForEN = false;
        // this._translate.use(this.language);
        //let test = super.user.name;
    }

}